package app

import (
	"strings"

	goflag "flag"

	log "gitee.com/youkelike/zlog"
	"github.com/spf13/pflag"
)

func WordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
	if strings.Contains(name, "_") {
		nname := strings.ReplaceAll(name, "_", "-")
		log.Warnf("%s is DEPRECATED. Use %s instead.", name, nname)

		return pflag.NormalizedName(nname)
	}
	return pflag.NormalizedName(name)
}

func initFlags() {
	pflag.CommandLine.SetNormalizeFunc(WordSepNormalizeFunc)
	pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
}

func PrintFlags(fs *pflag.FlagSet) {
	fs.VisitAll(func(f *pflag.Flag) {
		log.Debugw("FLAG: --%s=%q", f.Name, f.Value)
	})
}
